#include "system_icon_provider.h"
#include <QDebug>
#include <QFileInfo>
#include <QDir>
#include <QPixmap>
#include <QPainter>
#include <QBitmap>
#include <QFileIconProvider>

SystemIconProvider::SystemIconProvider(QObject *parent)
    : QObject(parent)
{
}

QString SystemIconProvider::getFileIconPath(const QString &filePath)
{
    QFileInfo info(filePath);
    if (info.isDir()) {
        return "folder";
    }
    
    QString suffix = info.suffix().toLower();
    if (suffix.isEmpty()) {
        return "file";
    }
    
    return suffix;
}

QString SystemIconProvider::getFileIconSource(const QString &filePath)
{
    QFileInfo info(filePath);
    if (info.isDir()) {
        return "qrc:/icons/folder.png";  // 文件夹图标
    }
    
    QString suffix = info.suffix().toLower();
    return getIconResourcePath(suffix);
}

QIcon SystemIconProvider::getFileIcon(const QString &filePath)
{
    QFileInfo info(filePath);
    if (info.isDir()) {
        return QIcon(":/icons/folder.png");
    }
    
    QString suffix = info.suffix().toLower();
    QString iconPath = getIconResourcePath(suffix);
    return QIcon(iconPath);
}

QString SystemIconProvider::getFileTypeDescription(const QString &filePath)
{
    QFileInfo info(filePath);
    
    if (info.isDir()) {
        return "文件夹";
    }
    
    // 基于扩展名的描述
    QString suffix = info.suffix().toLower();
    if (suffix.isEmpty()) {
        return "文件";
    }
    
    // 常见文件类型的中文描述
    QMap<QString, QString> typeMap;
    typeMap["txt"] = "文本文档";
    typeMap["doc"] = "Word 文档";
    typeMap["docx"] = "Word 文档";
    typeMap["pdf"] = "PDF 文档";
    typeMap["jpg"] = "JPEG 图像";
    typeMap["jpeg"] = "JPEG 图像";
    typeMap["png"] = "PNG 图像";
    typeMap["gif"] = "GIF 图像";
    typeMap["bmp"] = "BMP 图像";
    typeMap["mp3"] = "MP3 音频";
    typeMap["wav"] = "WAV 音频";
    typeMap["mp4"] = "MP4 视频";
    typeMap["avi"] = "AVI 视频";
    typeMap["zip"] = "ZIP 压缩包";
    typeMap["rar"] = "RAR 压缩包";
    typeMap["exe"] = "可执行文件";
    typeMap["dll"] = "动态链接库";
    
    return typeMap.value(suffix, suffix.toUpper() + " 文件");
}

QString SystemIconProvider::getFileTypeIcon(const QString &fileType)
{
    return getIconResourcePath(fileType.toLower());
}

QString SystemIconProvider::getIconResourcePath(const QString &fileType)
{
    // 使用PNG图标文件
    QMap<QString, QString> iconMap;
    iconMap["txt"] = "qrc:/icons/txt.png";
    iconMap["doc"] = "qrc:/icons/doc.png";
    iconMap["docx"] = "qrc:/icons/docx.png";
    iconMap["pdf"] = "qrc:/icons/pdf.png";
    iconMap["jpg"] = "qrc:/icons/jpg.png";
    iconMap["jpeg"] = "qrc:/icons/jpeg.png";
    iconMap["png"] = "qrc:/icons/png.png";
    iconMap["gif"] = "qrc:/icons/gif.png";
    iconMap["bmp"] = "qrc:/icons/bmp.png";
    iconMap["mp3"] = "qrc:/icons/mp3.png";
    iconMap["wav"] = "qrc:/icons/wav.png";
    iconMap["avi"] = "qrc:/icons/avi.png";
    iconMap["zip"] = "qrc:/icons/zip.png";
    iconMap["rar"] = "qrc:/icons/rar.png";
    iconMap["exe"] = "qrc:/icons/exe.png";
    iconMap["dll"] = "qrc:/icons/dll.png";
    
    // 如果找到对应的图标，返回图标路径
    if (iconMap.contains(fileType)) {
        return iconMap[fileType];
    }
    
    // 默认文件图标
    return "qrc:/icons/txt.png";
}

